
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace NCS_CS
{
	internal class CCardComboPci : _CCard
	{
		byte b1;
		byte B2;
        //byte b3;
		bool islinked;
		CCard9111 c9111;
		CCard6208 c6208;
        //byte b4;
		double stime;

        string m_errmsg="";
		private bool CCard_closeVLV2440(int v)
		{
			bool functionReturnValue = false;//todo
            if (v >= 0 & v <= 7)
            {
                b1 = ModMain.BitClearChar(ref b1, (byte)v);
                c9111.newDO( 0,  b1 + Convert.ToInt32(B2) * 256);
                functionReturnValue = true;
            }
            else if (v >= 8 & v <= 15)
            {
                B2 = ModMain.BitClearChar(ref B2,(byte) (v - 8));
                c9111.newDO( 0,  b1 + Convert.ToInt32(B2) * 256);
                functionReturnValue = true;
            }
            else if (v >= 16 & v <= 23)
            {
                B2 = ModMain.BitClearChar(ref B2, (byte)(v - 16));
                newDO( 2,  B2);
                functionReturnValue = true;
            }
            else if (v >= 24 & v <= 31)
            {
                B2 = ModMain.BitClearChar(ref B2, (byte)(v - 24));
                newDO( 3,  B2);
                functionReturnValue = true;
            }
			return functionReturnValue;
		}
		bool _CCard.closeVLV2440(int v)
		{
			return CCard_closeVLV2440(v);
		}
		private byte byte1 {
			get { return b1; }
		}
		byte _CCard.Byte1 {
			get { return byte1; }
		}
		private byte byte2 {
			get { return B2; }
		}
		byte _CCard.Byte2 {
			get { return byte2; }
		}
        //private byte byte3 {
        //    get { return b3; }
        //}
        //byte _CCard.Byte3 {
        //    get { return byte3; }
        //}
		private string CCard_errmsg {
			get { return m_errmsg; }
		}
		string _CCard.errmsg {
			get { return CCard_errmsg; }
		}


		private bool CCard_islinked {
			get { return islinked; }
		}
		bool _CCard.islinked {
			get { return CCard_islinked; }
		}

		private bool CCard_getchanelsVolts(ref int[] ch, ref double[] volts, int n, ref double t)
		{
			t = ModMain.myTimer() - stime;
			int i = 0;
			for (i = 0; i <= n - 1; i++) {
				volts[i] = ccard_getchanelVolt(Convert.ToInt32(ch[i]));
			}
            return true;
		}
		bool _CCard.getchanelsVolts(ref int[] ch, ref double[] volts, int n, ref double t)
		{
			return CCard_getchanelsVolts(ref ch, ref volts, n, ref t);
		}

		private double ccard_getchanelVolt(int chanel)
		{
			return c9111.getchanelVolt( chanel);
		}
		double _CCard.getchanelVolt(int chanel)
		{
			return ccard_getchanelVolt(chanel);
		}

		private bool CCard_getdi(int chanel)
		{
            //int v = 0;
            //c9111.Di(0,  ref v);

            //return ModMain.BitIsSet(v, chanel);
            return false;
		}
		bool _CCard.getdi(int chanel)
		{
			return CCard_getdi(chanel);
		}

		private bool CCard_getVlv(int v)
		{
			return getBit( v);
		}
		bool _CCard.getVlv(int v)
		{
			return CCard_getVlv(v);
		}

		private void ccard_link()
		{
			try {
				if (ModMain.program1.demo) {
					islinked = true;
				} else {
					c9111.link();
					c6208.link();
					if (c9111.islinked & c6208.islinked) {
						islinked = true;
					} else {
						islinked = false;
					}
				}
				return;
			} catch (System.Exception e) {
				islinked = false;
				//RaiseEvent myErr(1, "can not link card!")
				modLog.savelogmsg(ModMain.gtran.MyTranslate("combo采集卡 init ", "combo card init"));
				modLog.savelogmsg(e.ToString()); 
			}
		}
		void _CCard.link()
		{
			ccard_link();
		}

		private void ccard_newDA(int chanel, double v)
		{

			switch (chanel) {
				case 8://功率
					c9111.newDA(0, v);
					break;
                default :// 2=O zero 3=O2 zero 4=N zero 
					c6208.newDA(chanel, v);
					break;

			}
		}
		void _CCard.newDA(int chanel, double v)
		{
			ccard_newDA(chanel, v);
		}

		private bool CCard_openVLV2440(int v)
		{
			bool functionReturnValue = false;
            if (v >= 0 & v <= 7)
            {
                b1 = ModMain.BitSetChar(ref b1, (byte)v);
                //Call do9111(b1 + b2 * 256)
                //Call newDO(0, b1)
                int tmp;
                tmp = b1 + Convert.ToInt32(B2) * 256;
                c9111.newDO(0,  tmp);
                functionReturnValue = true;
            }
            else if (v >= 8 & v <= 15)
            {
                B2 = ModMain.BitSetChar(ref B2,(byte)( v - 8));
                //Call do9111(b1 + b2 * 256)
                //Call newDO(1, b2)
                c9111.newDO( 0,  b1 + Convert.ToInt32(B2) * 256);
                functionReturnValue = true;
            }
            else if (v >= 16 & v <= 23)
            {
                B2 = ModMain.BitSetChar(ref B2,(byte)( v - 16));
                //Call do9111(b1 + b2 * 256)
                newDO( 2,  B2);
                functionReturnValue = true;
            }
            else if (v >= 24 & v <= 31)
            {
                B2 = ModMain.BitSetChar(ref B2,(byte) (v - 24));
                //Call do9111(b1 + b2 * 256)
                newDO( 3,  B2);
                functionReturnValue = true;
            }
			return functionReturnValue;
		}
		bool _CCard.openVLV2440(int v)
		{
			return CCard_openVLV2440(v);
		}


		private void CCard_setCardAddr(int a)
		{
		}
		void _CCard.setCardAddr(int a)
		{
			CCard_setCardAddr(a);
		}

		private void CCard_shutdown()
		{
			b1 = 0;
			B2 = 0;
			newDO( 0,   b1);
			newDO( 1,   B2);
		}
		void _CCard.shutdown()
		{
			CCard_shutdown();
		}

		private void CCard_starttime()
		{
			stime = ModMain.myTimer();
		}
		void _CCard.starttime()
		{
			CCard_starttime();
		}

		private void ccard_unLink()
		{
			try {
				c9111.unLink();
				c6208.unLink();
				return;
			} catch (System.Exception e) {
				modLog.savelogmsg(e.ToString()); 
			}
		}
		void _CCard.unLink()
		{
			ccard_unLink();
		}


		public CCardComboPci() : base()
		{
			c9111 = new CCard9111();
			c6208 = new CCard6208();
		}


		public void link()
		{
			try {
				c9111.link();
				c6208.link();
				if (c9111.islinked & c6208.islinked) {
					islinked = true;
				} else {
					islinked = false;
				}
				return;
			} catch (System.Exception e) {
				islinked = false;
				// RaiseEvent myErr(1, "can not link card!")
				modLog.savelogmsg(ModMain.gtran.MyTranslate("combo 采集卡 init ", "combo card init"));
				modLog.savelogmsg(e.ToString()); 
			}
		}
		public void unLink()
		{
			try {
				c9111.unLink();
				c6208.unLink();
				return;
			} catch (System.Exception e) {
				modLog.savelogmsg(e.ToString()); 
			}
		}
		public void newDO( byte chanel,  byte v)
		{
            c9111.newDO( chanel,  Convert.ToInt32(v));
		}
		public bool getBit( int i)
		{
            if (i >= 0 & i <= 7)
            {
                return ModMain.BitIsSetChar(b1,(byte) i);
            }
            else if (i >= 8 & i <= 15)
            {
                return ModMain.BitIsSetChar(B2,(byte) (i - 8));
            }
			return false;
		}
	}
}
